# -*- coding: utf-8 -*-
"""
Created on Wed Oct 16 22:10:55 2019

@author: shael
"""

import mysql.connector
import numpy as np
from mysql.connector import Error
import pandas as pd
import plotly.graph_objects as go
from plotly.subplots import make_subplots
from plotly.offline import plot

try:
    mydb=mysql.connector.connect(
            
            host="bmckean.dynu.net",
            user="msurf-script",
            passwd="Capstone#2019",
            database="metasurface_design"
    
            )
    mycursor=mydb.cursor()
    print(mydb.is_connected())
except Error as e:
    print(e)
try:
            query="SELECT * from dogbone_simulations;" 
            mycursor.execute(query)
            results=mycursor.fetchall()
            mydb.commit()
            tab_name="dogbone_simulations"
            query="SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = \"%s\";" %(tab_name) 
            mycursor.execute(query)
            colsqur=mycursor.fetchall()
            cols=[]
            for col in colsqur:
                col=str(col[0])
                cols.append(col)
                print(col)
            mydb.commit()
except Error as e:
            print("Error reading data from MySQL table", e)
    
df=pd.DataFrame(results)
df.columns=cols

df = df[df['substrate_material']=="Rogers RO3010"]

topsdf=df.loc[df['location']=='top'].sort_values(['width'])
midsdf=df.loc[df['location']=='mid'].sort_values(['width'])
botsdf=df.loc[df['location']=='bot'].sort_values(['width'])


fig = make_subplots(rows=3, cols=1, shared_xaxes=True, vertical_spacing=0.03,
                    subplot_titles=("Top Trace","Middle Trace","Bottom Trace"))

fig.update_layout(title_text="Width of Dogbone v Impedance")

fig.update_xaxes(title_text="Dogbone Width (mm)", row=3, col=1)

fig.update_yaxes(title_text="Im(Z)", row=1, col=1)
fig.update_yaxes(title_text="Im(Z)", row=2, col=1)
fig.update_yaxes(title_text="Im(Z)", row=3, col=1)

for name1, group1 in topsdf.groupby(['length']):
<<<<<<< HEAD
    group1 = group1.round(6).groupby(['width']).apply(lambda grp: grp[grp['delta_s']==grp['delta_s'].min()])
    fig.append_trace(go.Scatter(
        x=group1['width'],
        y=group1['z'],
        name="L = "+str(name1)
    ), row=1, col=1)

for name1, group1 in midsdf.groupby(['length']):
    group1 = group1.round(6).groupby(['width']).apply(lambda grp: grp[grp['delta_s']==grp['delta_s'].min()])
    fig.append_trace(go.Scatter(
        x=group1['width'],
        y=group1['z'],
        name="L = "+str(name1)
    ), row=2, col=1)
    
for name1, group1 in botsdf.groupby(['length']):
    group1 = group1.round(6).groupby(['width']).apply(lambda grp: grp[grp['delta_s']==grp['delta_s'].min()])
    fig.append_trace(go.Scatter(
        x=group1['width'],
        y=group1['z'],
        name="L = "+str(name1)
    ), row=3, col=1)
=======

plot(fig)
          

    
    

    
    
